import rclpy

from rclpy.node import Node

from chapt4_interfaces.srv import FaceDetector 

class Custoum(Node):
    def __init__(self,node_name):
        super().__init__(node_name)
        self.get_logger().info(f"{node_name} 启动")
        self.client=self.create_client(FaceDetector,'stu_service') # 客户端名字要和服务端保持以一致
        self.number=1
        
    # 客户端发送请求
    def send_request(self):
        # 1 判断服务端是否在线
        while(self.client.wait_for_service(1.0)) is False:
            self.get_logger().info('等待服务端上线')
        # 2 构造request
        request=FaceDetector.Request()
        request.number=self.number
        self.number+=1
        # 发送请求等待处理相应
        future=self.client.call_async(request) # 需要等待服务端处理完才会把结果放到future中
        
        def result_callback(result_future):
            response=result_future.result() # 获取相应
            self.show_response(response)

        future.add_done_callback(result_callback)


    # 客户端显示相应结果
    def show_response(self,response):
        self.get_logger().info(f"识别到姓名:{response.name}")

def main():
    rclpy.init()
    node=Custoum('stu_custoum')
    while input():
        node.send_request()
        node.number+=1
    rclpy.spin(node)

    rclpy.shutdown()